<template>
  <div class="container">
    <div class="main-content">
      <div class="left-description">
        <div class="tab">
          <div class="tab-item tab-item-selected" @click="switchTab(0)">最新问题</div>
          <el-divider direction="vertical"></el-divider>
          <div class="tab-item" @click="switchTab(1)">热门问题</div>

          <el-button plain type="primary" class="ask-btn" @click="askQuestion">在线提问</el-button>
        </div>

        <el-card class="question-list">
          <div class="single-question" v-for="(item,index) in questionList" :key="index">
            <div class="question-title">
              <div class="ask-icon"></div>
              <div class="text" @click="jumpToDetailPage(item)" @mouseenter="handleQuestionMouseEnter(index)">
                {{ item.title }}
              </div>
              <el-tag type="success" size="mini" effect="dark" v-for="(it,idx) in item.tag" :key="idx"
                      style="margin-left: 10px">{{ it }}
              </el-tag>
              <div class="question-data">
                <el-link type="info" :underline="false" :disabled="true" style="margin-right: 10px"><i
                    class="el-icon-view el-icon--right" style="margin-right: 5px"></i>{{ item.view }}
                </el-link>
                <el-link type="info" :underline="false" :disabled="true" style="margin-right: 10px"><i
                    class="el-icon-chat-square" style="margin-right: 5px"></i>{{ item.reply }}
                </el-link>
                <el-link type="info" :underline="false" :disabled="true">发表时间：{{ item.createTime }}</el-link>
              </div>
            </div>
            <div class="question-answer" :style="index === 0 ? 'display: block' : ''">
              <div class="answer-icon"></div>
              <div class="answer-info" v-if="item.username !== '' && item.username !== undefined">
                <img :src="item.avatar" alt=""
                     class="answer-user-avatar"/>
                <div class="answer-detail">
                  <div class="answer-user-info">
                    <div class="answer-username">{{ item.username }}</div>
                    <div class="answer-user-other-info">{{ item.hospital }}</div>
                    <div class="answer-user-other-info">{{ item.answerCreateTime }}</div>
                  </div>
                  <div class="answer-content">{{ item.answer }}</div>
                </div>
              </div>
              <div v-else>
                当前暂无已被采纳的回答。
              </div>
            </div>
            <el-divider></el-divider>
          </div>
        </el-card>
        <div cl ass="bottom-blank"></div>
      </div>
      <div class="blank"></div>
      <div class="right-recommend">
        <el-card>
          <div class="recommend-title">
            推荐频道
          </div>
          <div class="channel-item" v-for="(item,index) in channels" :key="index"
               @click="getQuestionListByType(nowTab,item)">{{ item }}
          </div>
        </el-card>
        <el-card style="margin-top: 15px">
          <div class="recommend-title">
            疾病热度排行榜
          </div>
          <div class="single-disease-data" v-for="(item,index) in hotDisease" :key="index">
            <div class="disease">
              <i class="number-icon" :style="`background-color:${item.color}`">{{ index + 1 }}</i>
              <div class="disease-name">{{ item.disease }}</div>
            </div>
            <div class="hot">🔥{{ item.hot }}</div>
          </div>
        </el-card>
        <el-card style="margin-top: 15px;flex: 1;overflow-y: auto">
          <div class="recommend-title">
            权威医生列表
          </div>
          <div v-if="doctorList.length > 0">
            <div class="single-doctor" v-for="(item,index) in doctorList" :key="index">
              <img :src="item.avatar" class="doctor-avatar">
              <div class="doctor-info">
                <div class="doctor-username" @click="openPrivateChatDialog(item.username)">{{ item.username }}</div>
                <div class="doctor-other-info">所属医院：{{ item.hospital }}</div>
                <div class="doctor-other-info">手机号：{{ item.phone }}</div>
              </div>
              <div class="adoption-count">回答被采纳次数：{{ item.adoptionCount }}</div>
            </div>
          </div>
          <div v-else>
            <el-empty></el-empty>
          </div>
        </el-card>
        <div class="bottom-blank"></div>
      </div>
    </div>

    <el-dialog
        title="私信留言"
        :visible.sync="showPrivateChatDialog"
        width="30%">
      <el-form v-model="privateMessageForm" label-width="auto">
        <el-form-item label="留言内容">
          <el-input
              resize="none"
              type="textarea"
              :rows="2"
              placeholder="请输入留言内容"
              v-model="privateMessageForm.content">
          </el-input>
        </el-form-item>
        <el-form-item label="附件">
          <el-upload
              drag
              action="https://yiyuy.mynatapp.cc/info/imageToOSS"
              :on-success="handlePrivateMessageUploadSuccess"
              :limit="1">
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="showPrivateChatDialog = false">取 消</el-button>
        <el-button type="primary" @click="submitPrivateMessage">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 在线提问 -->
    <el-dialog
        title="在线提问"
        :visible.sync="showUploadQuestion"
        width="30%">
      <el-form :model="questionForm" label-width="auto" :rules="questionFormRules">
        <el-form-item label="问题标题" prop="title">
          <el-input v-model="questionForm.title" placeholder="请输入问题标题"></el-input>
        </el-form-item>
        <el-form-item label="问题内容" prop="content">
          <el-input
              resize="none"
              type="textarea"
              :rows="2"
              placeholder="请输入问题内容"
              v-model="questionForm.content">
          </el-input>
        </el-form-item>
        <el-form-item label="上传附件">
          <el-upload
              :limit="1"
              drag
              action="https://yiyuy.mynatapp.cc/info/imageToOSS"
              :on-exceed="handleExceed"
              :on-success="handleUploadSuccess"
              :on-error="handleUploadError"
              :on-progress="handleUploadProgress"
          >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          </el-upload>
        </el-form-item>
        <el-form-item label="疾病标签" prop="tag">
          <el-select v-model="questionForm.tag" multiple placeholder="请选择疾病标签">
            <el-option
                v-for="(item,index) in channels"
                :key="index"
                :label="item"
                :value="item">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="button-group">
        <el-button @click="showUploadQuestion = false">取 消</el-button>
        <el-button type="primary" @click="submitQuestion">确 定</el-button>
      </div>

    </el-dialog>
  </div>
</template>

<script>
import {
  getDiseaseHotList,
  getPositiveDoctorList,
  getQuestionList,
  leaveMessage,
  submitNewQuestion,
  updateView
} from "@/api/question";

export default {
  name: "OnlineConsultation",
  data() {
    return {
      // 是否显示私信留言窗口
      showPrivateChatDialog: false,
      // 是否显示 在线提问 窗口
      showUploadQuestion: false,
      // 私信留言需要填写的表格
      privateMessageForm: {
        content: '',
        fileUrl: '',
        toUsername: '',
      },
      questionForm: {
        title: '',
        content: '',
        fileUrl: '',
        tag: ''
      },
      questionFormRules: {
        title: [
          {required: true, message: '请输入标题', trigger: 'blur'},
        ],
        content: [
          {required: true, message: '请输入内容', trigger: 'blur'},
        ],
        tag: [
          {required: true, message: '请选择标签', trigger: 'change'},
        ]
      },
      questionList: [],
      // 当前是那个导航栏
      nowTab: 0,
      // 当前浏览的第几条问题
      nowQuestion: 0,
      channels: ['糖尿病视网膜病变', '青光眼', '白内障', 'AMD', '高血压', '近视', '其他疾病/异常'],
      hotDisease: [],
      doctorList: []
    }
  },
  mounted() {
    this.getQuestionListByType(0, '');
    this.getDiseaseHot();
    this.getPositiveDoctor();
  },
  methods: {
    handlePrivateMessageUploadSuccess(response, file, fileList) {
      this.privateMessageForm.fileUrl = response.data;
      this.$message.success('文件上传成功')
    },
    // 鼠标移入问题 显示当前问题采纳的答案 同时取消上一个问题答案
    handleQuestionMouseEnter(index) {
      if (index === this.nowQuestion) {
        return;
      }

      var elements = document.querySelectorAll('.question-answer');
      elements[this.nowQuestion].style.display = 'none';
      elements[index].style.display = 'block';
      this.nowQuestion = index;
    },
    handleExceed() {
      this.$message.warning('只能上传一个文件')
    },
    handleUploadProgress() {
      this.$message.warning('文件上传中')
    },
    handleUploadError() {
      this.$message.error('上传文件出错，请联系管理员')
    },
    handleUploadSuccess(res) {
      this.$message.success('文件上传成功...')
      this.questionForm.fileUrl = res.data;
    },
    // 点击跳转至具体的问题详情页面
    jumpToDetailPage(item) {
      // 跳转之前 增加该问题点击量
      updateView(item.id);
      this.$router.push({path: '/home/questionDetail', query: {item: item}})
    },
    // 获取疾病热度排行榜
    getDiseaseHot() {
      getDiseaseHotList().then(res => {
        // 根据每种疾病的热度从高到低排序
        let arr = res.data.sort((a, b) => b.hot - a.hot)
        for (let i = 0; i < arr.length; i++) {
          this.hotDisease.push(arr[i])
          if (i === 0) this.hotDisease[i].color = '#ff5d58'
          else if (i === 1) this.hotDisease[i].color = '#fa7a1b'
          else if (i === 2) this.hotDisease[i].color = '#ffb81d'
          else this.hotDisease[i].color = '#c7c7c7'
        }
      })
    },
    // 获取权威医生列表
    getPositiveDoctor() {
      getPositiveDoctorList().then(res => {
        this.doctorList = res.data.sort((a, b) => b.adoptionCount - a.adoptionCount)
      })
    },
    // 获取问题列表
    getQuestionListByType(type, tag) {
      getQuestionList(type, tag).then(res => {
        this.questionList = res.data;
        // 拿到问题列表后对tag进行处理
        for (let i = 0; i < this.questionList.length; i++) {
          let arr = this.questionList[i].tag.split(',')
          this.questionList[i].tag = []
          for (let j = 0; j < arr.length - 1; j++) this.questionList[i].tag.push(arr[j])
        }
      })
    },
    // 切换tab栏
    switchTab(index) {
      if (this.nowTab === index) {
        return;
      }
      var elements = document.querySelectorAll('.tab-item');
      elements[this.nowTab].className = 'tab-item';
      elements[index].className = 'tab-item tab-item-selected';
      this.nowTab = index;
      this.getQuestionListByType(index, '');
    },
    // 点击在线提问按钮
    askQuestion() {
      this.showUploadQuestion = true;
      this.questionForm = {
        question: '',
        title: '',
        content: '',
        fileUrl: '',
        tag: ''
      }
    },
    // 提交问题
    submitQuestion() {
      this.showUploadQuestion = false;
      // 上传之前对tag做一个处理
      let arr = this.questionForm.tag
      this.questionForm.tag = ''
      for (let i = 0; i < arr.length; i++) this.questionForm.tag += arr[i] + ','
      submitNewQuestion(this.questionForm).then(res => {
        this.$message.success('问题发表成功');
        this.getQuestionListByType(this.nowTab, '');
      })
    },
    // 打开私信留言窗口
    openPrivateChatDialog(username) {
      this.privateMessageForm = {
        content: '',
        fileUrl: '',
        toUsername: '',
      }
      this.privateMessageForm.toUsername = username
      this.showPrivateChatDialog = true;
    },
    submitPrivateMessage() {
      leaveMessage(this.privateMessageForm).then(res => {
        this.$message.success('留言成功')
        this.showPrivateChatDialog = false;
      })
    }
  }
}
</script>

<style lang="less" scoped>
@keyframes MoveIn {
  0% {
    opacity: 0;
    transform: translateY(12px);
  }

  100% {
    opacity: 1;
    transform: translateY(0);
  }
}

.container {
  width: 100%;
  height: 100%;
  animation: MoveIn .7s cubic-bezier(.4, 0, .2, 1);

  .main-content {
    width: 96%;
    height: 96%;
    margin: 15px auto;
    display: flex;
    padding: 0;

    .left-description {
      width: 71%;
      height: 100%;
      display: flex;
      flex-direction: column;

      .tab {
        width: 100%;
        background-color: #fff;
        box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
        display: flex;
        align-items: center;
        border-radius: 4px;
        position: relative;

        .tab-item {
          position: relative;
          text-align: center;
          padding: 0 30px;
          font-size: 16px;
          font-weight: 400;
          color: #333;
          cursor: pointer;
          line-height: 50px;
        }

        .tab-item-selected {
          position: relative;
          border: 1px solid #055ad7;
          font-size: 18px;
          font-weight: 600;
          color: #055ad7;
          line-height: 48px;
        }

        .tab-item-selected:before {
          content: "";
          display: block;
          position: absolute;
          background: rgba(0, 0, 0, 0);
          top: 100%;
          left: 50%;
          width: 0;
          height: 0;
          border-left: 8px solid rgba(0, 0, 0, 0);
          border-right: 8px solid rgba(0, 0, 0, 0);
          border-top: 9px solid #055ad7;
          transform: translateX(-50%);
        }

        .tab-item-selected:after {
          content: "";
          display: block;
          width: 16px;
          height: 48px;
          background: -webkit-gradient(linear, right top, left top, color-stop(50%, hsla(0, 0%, 100%, 0)), to(#9cc1ef));
          background: -webkit-linear-gradient(right, hsla(0, 0%, 100%, 0) 50%, #9cc1ef);
          background: -moz-linear-gradient(right, hsla(0, 0%, 100%, 0) 50%, #9cc1ef 100%);
          background: -o-linear-gradient(right, hsla(0, 0%, 100%, 0) 50%, #9cc1ef 100%);
          background: linear-gradient(270deg, hsla(0, 0%, 100%, 0) 50%, #9cc1ef);
          opacity: .61;
          position: absolute;
          right: -16px;
          top: -1px;
        }

        .ask-btn {
          position: absolute;
          right: 10px;
        }
      }

      .question-list {
        margin-top: 15px;
        overflow-y: auto;
        flex: 1;

        .single-question {
          .question-title {
            display: flex;
            align-items: center;
            position: relative;

            .ask-icon:before {
              display: inline-block;
              content: '问';
              vertical-align: middle;
              width: 22px;
              height: 22px;
              background-color: #47b2ff;
              border-radius: 4px;
              color: #fff;
              font-size: 14px;
              line-height: 22px;
              text-align: center;
            }

            .text {
              margin-left: 10px;
              font-size: 18px;
              cursor: pointer;
            }

            .text:hover {
              color: #49B4FF
            }

            .question-data {
              display: flex;
              margin-left: auto;
              align-items: center;
            }
          }

          .question-answer {
            background-color: #f8f8f8;
            border-radius: 4px;
            margin-top: 15px;
            margin-bottom: 15px;
            padding: 20px 20px 15px;
            position: relative;
            display: none;

            .answer-icon:before {
              background-color: #fc8589;
              content: '答';
              position: absolute;
              left: 0;
              top: 0;

              width: 22px;
              height: 22px;
              border-radius: 4px;
              color: #fff;
              font-size: 14px;
              line-height: 22px;
              text-align: center;
            }

            .answer-info {
              width: 100%;
              display: flex;

              .answer-user-avatar {
                width: 52px;
                height: 52px;
                border-radius: 50%;
              }

              .answer-detail {
                flex: 1;
                margin-left: 15px;

                .answer-user-info {
                  display: flex;
                  align-items: center;

                  .answer-username {
                    color: #666666;
                    font-size: 16px;
                  }

                  .answer-user-other-info {
                    display: flex;
                    color: #999999;
                    font-size: 14px;
                    margin-left: 10px;
                  }
                }

                .answer-content {
                  margin-top: 5px;
                  overflow: hidden;
                  text-overflow: ellipsis;
                  display: -webkit-box;
                  -webkit-line-clamp: 2;
                  -webkit-box-orient: vertical;
                  line-clamp: 2;
                  font-size: 14px;
                  word-break: break-all;
                  word-wrap: break-word;
                }
              }
            }

          }
        }
      }
    }

    .blank {
      width: 1.5%;
    }

    .right-recommend {
      flex: 1;
      height: 100%;
      display: flex;
      flex-direction: column;

      .recommend-title {
        padding-left: 10px;
        color: #055ad7;
        font-size: 17px;
        font-weight: 700;
        position: relative;
        margin-bottom: 5px;
      }

      .recommend-title:before {
        content: "";
        display: block;
        width: 4px;
        height: 18px;
        background: #055ad7;
        position: absolute;
        left: 0;
        top: 4px;
      }

      .channel-item {
        line-height: 30px;
        border: 1px solid #eee;
        border-radius: 15px;
        padding: 0 10px;
        font-size: 14px;
        float: left;
        margin-bottom: 10px;
        margin-right: 10px;
        cursor: pointer;
      }

      .channel-item:hover {
        color: #49B4FF;
      }

      .single-disease-data {
        display: flex;
        width: 100%;

        .disease {
          width: 80%;
          line-height: 32px;
          font-size: 14px;
          display: flex;
          align-items: center;

          .number-icon {
            width: 16px;
            line-height: 16px;
            border-radius: 3px;
            background-color: #c7c7c7;
            display: inline-block;
            margin-right: 10px;
            color: #fff;
            font-size: 12px;
            text-align: center;
            font-style: normal;
          }
        }

        .hot {
          flex: 1;
          margin-left: auto;
          display: flex;
          align-items: center;
        }
      }

      .single-doctor {
        display: flex;
        align-items: center;

        .doctor-avatar {
          width: 40px;
          height: 40px;
          border-radius: 50%;
        }

        .doctor-info {
          margin-left: 10px;

          .doctor-username {
            font-size: 16px;
            cursor: pointer;
          }

          .doctor-username:hover {
            color: #49B4FF;
          }

          .doctor-other-info {
            margin-top: 5px;
            font-size: 12px;
            color: #999999;
          }
        }

        .adoption-count {
          margin-left: auto;
          font-size: 14px;
          font-weight: 600;
        }
      }

    }
  }
}

.bottom-blank {
  width: 100%;
  height: 30px;
}

.button-group {
  width: 40%;
  margin: 0 auto;
  display: flex;
  align-items: center;
}
</style>